package com.milo.thread;

import org.junit.Test;

/**
 * @author milo
 * @date 2019/4/2123:53
 * @description Thread类api学习
 */
public class ThreadApiTest {


    public static class MyThread extends Thread {

        private String name;

        public MyThread(String name) {
            super(name);
            this.name = name;

        }

        @Override
        public void run() {
            for (int i = 0; i < 2; i++) {
                System.out.println(Thread.currentThread().getName() + "--->" + i);
            }
        }
    }

    /**
     * 返回对当前正在执行的线程对象的引用
     */
    @Test
    public void test1() {
        System.out.println("当前线程：" + Thread.currentThread());
    }

    /**
     * 返回当前线程的线程组及其子组中活动线程数的估计值。
     */
    @Test
    public void test2() {
        System.out.println("" + Thread.activeCount());
    }

    /**
     * 测试当前线程是否已经中断,如果中断，返回true，否则false
     */
    @Test
    public void test3() {
        System.out.println("" + Thread.interrupted());
    }


    /**
     * 返回此Thread的标识符。
     */
    @Test
    public void test4() {
        MyThread thread = new MyThread("线程1");
        System.out.println("Thread的标识符:" + thread.getId());
    }

    /**
     * 返回此线程的名称。
     */
    @Test
    public void test5() {
        MyThread thread = new MyThread("可爱线程");
        System.out.println("线程名称:" + thread.getName());
    }

    /**
     * {@link Thread#sleep} 使当前执行的线程处于休眠状态
     */
    @Test
    public void test6() {
        System.out.println("Starting thread...");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Stopping application...");
    }

    /**
     * {@link Thread#interrupt() #}测试当前线程是否被中断
     */
    @Test
    public void test7() {

        new Thread() {
            @Override
            public void run() {
                //如果线程未被中断,返回false
                System.out.println("测试当前线程是否被中断:" + Thread.interrupted());
                interrupt();
                //线程被中断,返回true
                System.out.println("测试当前线程是否被中断:" + Thread.interrupted());
            }
        }.start();

    }

    /**
     * {@link Thread#getPriority()} 返回线程优先级
     */
    @Test
    public void test8() {
        MyThread thread = new MyThread("小淘气线程");
        int i = thread.getPriority();
        System.out.println("i======" + i);
    }

    /**
     * {@link Thread#setPriority(int)}} 更改线程优先级
     */
    @Test
    public void test9() {
        MyThread thread1 = new MyThread("马云");
        //1~10   1最低  10最高    5是默认值
        thread1.setPriority(7);
        MyThread thread2 = new MyThread("王老吉");
        MyThread thread3 = new MyThread("刘强东");
        thread3.setPriority(3);
        thread1.start();
        thread2.start();
        thread3.start();

    }

    /**
     * 设置线程名称
     */
    @Test
    public void test10() {
        MyThread thread1 = new MyThread("马云");
        System.out.println("线程名称:" + thread1.getName());
        thread1.setName("milo");
        System.out.println("线程名称:" + thread1.getName());
    }

    /**
     * {@link Thread#isDaemon()}检查当前线程是否为守护线程
     * {@link Thread#setDaemon(boolean)}将某个线程设置为守护线程,必须在start()方法之前调用
     */
    @Test
    public void test11() {
        MyThread thread1 = new MyThread("milo");
        boolean daemon1 = thread1.isDaemon();
        System.out.println("当前线程是否为守护线程:" + daemon1);
        thread1.setDaemon(true);
        boolean daemon2 = thread1.isDaemon();
        System.out.println("当前线程是否为守护线程:" + daemon2);

    }

}
